package com.yanqu.road.entity.activity.suitang.data;

import com.yanqu.road.entity.DataObject;
import com.yanqu.road.entity.DataStatus;
import com.yanqu.road.entity.FieldTypeAnnotation;
import com.yanqu.road.entity.activity.suitang.entity.union.CrossSuiTangUnionJoinMember;
import com.yanqu.road.entity.enums.eUnionPosition;
import com.yanqu.road.entity.union.UnionBaseInfo;

import java.sql.Types;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@FieldTypeAnnotation(tableName = "t_c_suitang_union_join_data")
public class CrossSuiTangUnionJoinData extends DataObject {

    @FieldTypeAnnotation(desc = "活动ID", select = true, update = true)
    private int activityId;

    @FieldTypeAnnotation(desc = "活动分组ID", select = true, update = true)
    private int groupId;

    @FieldTypeAnnotation(desc = "商会UID", update = true)
    private String unionUid = "";
    @FieldTypeAnnotation(desc = "商会信息", sqlType = Types.LONGVARCHAR)
    private UnionBaseInfo unionBaseInfo;
    @FieldTypeAnnotation(desc = "商会成员", sqlType = Types.LONGVARCHAR)
    private Map<Long, CrossSuiTangUnionJoinMember> memberMap = new ConcurrentHashMap<>();

    // 缓存，门客赚钱
    private long totalAbility = 0L;

    // ========================================================================
    public int getActivityId() {
        return activityId;
    }
    public void setActivityId(int activityId) {
        if (this.activityId != activityId) {
            this.activityId = activityId;
            setOp(DataStatus.Update);
        }
    }

    public int getGroupId() {
        return groupId;
    }
    public void setGroupId(int groupId) {
        if (this.groupId != groupId) {
            this.groupId = groupId;
            setOp(DataStatus.Update);
        }
    }

    public String getUnionUid() {
        return unionUid;
    }
    public void setUnionUid(String unionUid) {
        if (unionUid == null){
            return;
        }
        if (this.unionUid == null || !this.unionUid.equals(unionUid)) {
            this.unionUid = unionUid;
            setOp(DataStatus.Update);
        }
    }

    public UnionBaseInfo getUnionBaseInfo() {
        return unionBaseInfo;
    }
    public void setUnionBaseInfo(UnionBaseInfo unionBaseInfo) {
        if (null == unionBaseInfo){
            return;
        }
        if (this.unionBaseInfo == null || !this.unionBaseInfo.equals(unionBaseInfo)) {
            this.unionBaseInfo = unionBaseInfo;
            setOp(DataStatus.Update);
        }
    }

    public java.util.Map<java.lang.Long, com.yanqu.road.entity.activity.suitang.entity.union.CrossSuiTangUnionJoinMember> getMemberMap() {
        return memberMap;
    }
    public void setMemberMap(java.util.Map<java.lang.Long, com.yanqu.road.entity.activity.suitang.entity.union.CrossSuiTangUnionJoinMember> memberMap) {
        if (null == memberMap){
            return;
        }
        if (this.memberMap == null || !this.memberMap.equals(memberMap)) {
            this.memberMap = memberMap;
            setOp(DataStatus.Update);
        }
    }

    // ==============================

    public long getTotalAbility() {
        return totalAbility;
    }

    public void setTotalAbility(long totalAbility) {
        this.totalAbility = totalAbility;
    }

    // ==============================

    public void reCalcAbility() {
        long totalAbility = getMemberMap().values().stream().mapToLong(CrossSuiTangUnionJoinMember::getTopAbility).sum();
        setTotalAbility(totalAbility);
    }

    public long getMasterId() {
        for (CrossSuiTangUnionJoinMember member : this.memberMap.values()) {
            if (member.getPosition() == eUnionPosition.Master.getValue()) {
                return member.getUserId();
            }
        }
        return unionBaseInfo.getMasterUserId();
    }
}
